package com.studio5704website.api.exception.handler;


import com.studio5704website.api.exception.*;
import com.studio5704website.core.httpResult.HttpResult;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * @author 高威
 * 接口异常统一处理
 *
 */
@RestControllerAdvice
public class CustomizeExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler({ApiException.class})
    public ResponseEntity<?> handleInvalidResponse(RuntimeException e, WebRequest request) {
        ApiException apiException = (ApiException) e;
        HttpResult result = apiException.getResult();
        HttpHeaders headers = new HttpHeaders();
        HttpStatus status = HttpStatus.valueOf(result.getStatusCode());
        headers.setContentType(MediaType.APPLICATION_JSON);
        return handleExceptionInternal(e, result, headers, status, request);
    }
}